Okay, so last lesson we learned about a type of function.
That allows you to do some common do some commonly needed tasks such as cloning and destroying.
Just by using a line.
Okay.
And these are functions that have already been made by Roblox studio.
But what happens if you want to make your own functions?
Well, we can have a look at that today okay.
So go ahead and add a script into the workspace.
And we're going to have we're going to learn about creating our own functions.
Now functions is a word you've heard a lot.
But what actually is a function.
Well a function is actually very very similar to a variable okay.
With a variable you start off with the keyword local.
You then give it a name.
So part you, then you, you then make that equal to something.
Which will be a data type such as game, that workspace part, which is an instance data type.
Okay.
Very sorry.
Not variables.
Functions are similar, but instead of making them equal to one bit of data, you can make them equal
to lines upon lines of code.
Okay.
And the way you create them is actually quite similar to the way you create variables.
It might be worth just revisiting the variables lecture before you go on to this lesson.
So to create a function you first off use a keyword local okay.
Then you want to specify that you are creating a function.
So space after that and then write the keyword function.
Okay.
Another space.
And here you write down the name of your function.
So variables have their own names.
So do functions.
And when you use this name.
That name will basically represent all of the code you stored inside of it.
Okay.
Um now the the I won't talk about the, the rules for the names of the functions because the rules are
the exact same as they are for variables.
Okay.
So again, I would recommend maybe going back to that lesson just so you can have a recap of those,
but the rules are the exact same.
Okay.
Um, so let's just call this my first function.
Okay.
Put a pair of brackets or parentheses afterwards and then press enter okay.
And that is a format for your function.
Okay.
Um, now these um these parentheses or brackets at the end here are very typical for whenever you use
the name of a function.
And we will look at that soon.
But for now again you just kind of need to know that they're there for a reason.
But in in a few lessons time we will learn about how they work.
Okay.
Um, so let's, let's get this function to do something.
So all I'm going to get this function to do is just to print a message into the output.
So we know when the function actually worked.
So let's just print um function ran okay.
So let's play the game and see if the function works.
And nothing happens in the output.
Okay.
And there's a very key reason for this.
Okay.
Um, is that, um, our function has been created, but we never run the function.
We never used it.
Okay.
Like how with variables.
This bit here is creating the variable, but you wouldn't actually use the variable until you say its
name.
Exact same with functions.
Okay.
The function won't run.
It won't run any of your code inside of it unless you say its name.
So you need to actually say the my first function like this or whatever it's called.
Or again, a pair of brackets at the end and that will then run your function.
So if we try to run the game again it will hopefully print function run just there.
Okay.
So that's the very simple form for functions.
Okay.
Now you can write whatever code you would like inside of a function.
You can put variables inside of functions.
You can put loops and statements inside of functions.
You can put anything you like, even another function inside of a function you can do.
Okay.
Um, so they are such a useful tool for you when you are, when you are coding anything really.
Now to start off with, they don't seem too useful because you kind of think, oh, what's the point
of doing all this?
Well, I could just say print function run instead.
Like that's fair.
Um, but because this is.
But the reason is because this is the most basic level of a function.
Okay.
And in the next few lessons, we will be adding to this and making them more and more useful until we
get to do things that you wouldn't really be able to do, um, without functions.
So believe me, they are much more useful than this.
This is just their kind of base use.
Okay.
Um, but I guess the only real use of using it like this would be to block out sections of your code
to do certain tasks.
So maybe you can run.
Um, let's say, I don't know, you wanted to, um, make the player completely invisible.
Okay.
And you could put it as a function.
So it's nice and simple.
You could write your code and then you could say, when we're doing the invisibility potion, um, prints
or sorry, return.
Um, then when you want to actually use the function in that bit there.
Okay.
So that's one use of this.
But again, there's many more uses when we add stuff onto it.
So I'm going to show you an example.
Um, another example of functions.
And for this one we are going to create a function which adds a part into our game with certain properties.
Okay.
So I'm going to call this add part.
Like that there pair of brackets and press enter.
Okay.
And to add a part into our game you use instance dot new.
So let's say local new part is equal to instance dot new.
And um let's put in there let's put this part inside of a model actually.
So let's say instance dot new model into game dot workspace.
Let's say um let's call this new model new model dot name is equal to part model.
Then we can add in our part.
So local new part is equal to instance dot new part into the um the new model okay.
New part dot.
Um let's change properties here.
Let's go for anchored is equal to true.
Let's say new part dot color is equal to color three dot new.
And let's pick a color here.
Let's go for a green color.
New part dot material is equal to enum dot material dot.
Um let's go for um let's use only proper terrain wood planks okay.
And let's also say new parts dot shape is equal to enum dot material.
Sorry not material here you here um enum dot part type dot corner wedge.
Okay.
So this adds our part in with those given properties okay.
So now I've done that.
Whenever we say add part it will add that part into our game.
So let's go over to the workspace.
And we can see it's added our part model.
And inside of it we've got our parts here.
It's put it here because it's supposed to be at 000.
If we were to move it a bit you could see it just there.
Okay.
And the cool thing about this is you can run this function three times and it will add three different
parts in.
Whereas previously you might have to write all this code out three different times.
Okay.
So that's just a basic example on how functions work now.
Have a tryout with ease, but I promise you, they will become a lot more useful when we learn a bit
more about them in our next lessons.
Okay, so have a try out with those and join me in the next lesson where we'll look about how we can
change these around and make them actually a lot more useful to us.
Okay.